package encrypt;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.tomcat.util.codec.binary.Base64;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import pojo.User;

@Component
@Aspect
public class EncryptMd5 {
	
	public EncryptMd5() throws NoSuchAlgorithmException {
		System.out.println("md5");
	}

	@Around(value="execution(* service.UserService.login(..))")
	public Object encrypt(ProceedingJoinPoint point) throws NoSuchAlgorithmException{
		//获得接入点传入的参数
			
				User user = (User) point.getArgs()[0];
				
				String pwd = user.getPwd();
				Object o= null;
				MessageDigest md5 = MessageDigest.getInstance("MD5");
				byte[] b = Base64.encodeBase64(md5.digest(pwd.getBytes()));
				System.out.println("注册加密加密");
				System.out.println(new String(b));
				user.setPwd(new String(b));
				try {
					
					o = point.proceed(new Object[]{user});
				} catch (Throwable e) {
					e.printStackTrace();
				}
				return o;
	}
	
	@Around(value="execution(* service.UserService.findByName(..))")
	public Object encryptPwd(ProceedingJoinPoint point) throws NoSuchAlgorithmException{
		//获得接入点传入的参数
			System.out.println("登录加密");
				String uname=(String) point.getArgs()[0];
				String pwd = (String) point.getArgs()[1];
				
				Object o= null;
				MessageDigest md5 = MessageDigest.getInstance("MD5");
				byte[] b = Base64.encodeBase64(md5.digest(pwd.getBytes()));
				System.out.println(new String(b));
				
				try {
					
					o = point.proceed(new Object[]{uname,new String(b)});
				} catch (Throwable e) {
					e.printStackTrace();
				}
				return o;
	}

}
